/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.snowstack.vendingmachine;

/**
 * @author icanci
 * @date Created in 2021/05/24 09:50
 * @version 1.0
 */
@SuppressWarnings("all")
public class Machine {
    // 机器本身包含所有的状态机
    /**
     * 售完
     */
    private State soldOutState;
    /**
     * 没有硬币
     */
    private State noQuarterState;
    /**
     * 有硬币
     */
    private State hasQuarterState;
    /**
     * 出售中
     */
    private State soldState;
    /**
     * 机器的当前状态
     */
    private State state;
    /**
     * 机器中当前糖果的数量
     */
    private int   count = 0;

    /**
     * 初始化机器，引入所有的状态机，初始化糖果数量，初始化机器状态
     * @param count
     */
    public Machine(int count) {
        this.soldOutState = new SoldOutState(this);
        this.noQuarterState = new NoQuarterState(this);
        this.hasQuarterState = new HasQuarterState(this);
        this.soldState = new SoldState(this);
        this.count = count;
        if (this.count > 0) {
            this.state = noQuarterState;
        }
    }

    /**
     * 释放糖果时的内部处理程序
     */
    public void releaseBall() {
        System.out.println("a gumball comes rolling out the solt...");
        if (count > 0) {
            count -= 1;
        }
    }

    /**
     * 加入硬币
     */
    public void insertQuerter() {
        state.insertQuarter();
    }

    /**
     * 根据摇动情况，处理摇动结果，返回处理结果，释放糖果
     */
    public void ejectQuarter() {
        state.ejectQuarter();
    }

    /**
     * 摇动转柄，并且释放糖果
     */
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }

    // 获取状态
    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getState() {
        return state;
    }

    public int getCount() {
        return count;
    }

    public void setState(State state) {
        this.state = state;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Machine{");
        sb.append("state=").append(state.getClass().getSimpleName());
        sb.append(", count=").append(count);
        sb.append('}');
        return sb.toString();
    }
}